﻿function ECommerceOrderViewModel(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var order = ko.observable(null);

    var productInfoRoute = context.spaRuntime.findRouteByName('product');

    function showFor(orderId) {
        var api = context.api();
        spinner.enter();
        api.orders.getById(orderId, function (data) {
            spinner.leave();
            order(new ECommerceOrder(data));
        }, function () {
            spinner.leave();
        });
    }

    function getProductUrl(product) {
        return productInfoRoute.formatHash({
            id: product.id()
        });
    }

    self.order = order;
    self.showFor = showFor;
    self.getProductUrl = getProductUrl;
}

function ECommerceOrder(orderInfo) {

    var self = this;

    var id = ko.observable('');

    var orderDate = ko.observable('');

    var totalPrice = ko.observable(0);

    var items = ko.observableArray([]);

    function findByProduct(prodId) {
        var cartItems = items();
        for (var i = 0; i < cartItems.length; i++) {
            var cartItem = cartItems[i];
            if (cartItem.id() == prodId) {
                return cartItem;
            }
        }
        return null;
    }

    function setInfo(info) {
        id(info.orderId);
        orderDate(info.orderDate);
        totalPrice(info.totalValue);

        var array = info.items;

        var res = [];
        for (var i = 0; i < array.length; i++) {
            var itemInfo = array[i];
            var item = new ECommerceOrderItem(itemInfo);
            res.push(item);
        }
        items(res);
    }

    if (orderInfo) {
        setInfo(orderInfo);
    }

    self.id = id;
    self.orderDate = orderDate;
    self.totalPrice = totalPrice;
    self.items = items;
    self.findByProduct = findByProduct;
    self.setInfo = setInfo;
}

function ECommerceOrderItem(itemInfo) {

    var self = this;

    var id = ko.observable('');

    var title = ko.observable('');

    var description = ko.observable('');

    var imageUrl = ko.observable('');

    var itemPrice = ko.observable(0);

    var count = ko.observable(0);

    var price = ko.computed(function () {
        return itemPrice() * count();
    });

    function fromProduct(product) {
        id(product.id());
        title(product.title());
        itemPrice(product.price());
        count(0);
    }

    function getInfo() {
        return {
            prodId: id(),
            prodName: title(),
            prodDesc: description(),
            prodPrice: itemPrice(),
            quantity: count(),
            imageUrl: imageUrl()
        };
    }

    function setInfo(info) {
        id(info.prodId);
        title(info.prodName);
        description(info.prodDesc);
        itemPrice(info.prodPrice);
        count(info.quantity);
        imageUrl(info.imageUrl);
    }

    if (itemInfo) {
        setInfo(itemInfo);
    }

    self.id = id;
    self.title = title;
    self.itemPrice = itemPrice;
    self.count = count;
    self.price = price;
    self.fromProduct = fromProduct;
    self.getInfo = getInfo;
}